package com.jianzh5.service.impl;

import com.jianzh5.common.DataPage;
import com.jianzh5.common.DataTables;
import com.jianzh5.dao.BaseDaoI;
import com.jianzh5.entity.TCase;
import com.jianzh5.page.PageUtil;
import com.jianzh5.page.PageView;
import com.jianzh5.pojo.PAdmin;
import com.jianzh5.pojo.PCase;
import com.jianzh5.service.CaseServiceI;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * <code>CaseServiceImpl</code>
 * </p>
 * 产品案例业务实现类
 * @author jianzhang11
 * @version 1.0 2016/12/8
 */
@Service("caseService")
public class CaseServiceImpl implements CaseServiceI{
    private final Logger logger = Logger.getLogger(CaseServiceImpl.class);
    /**
     * 注入产品案例Dao类
     */
    @Autowired
    private BaseDaoI<TCase> caseDao;

    @Override
    public DataTables dataTables(String order, String sort, DataPage dataPage) {
        String hql = "from TCase t";
        String totalHql = "select count(*) "+hql;
        hql += " order by t."+sort+" "+ order;
        List<TCase> tlist = caseDao.findByPosition(hql,dataPage.getStart(), dataPage.getLength());
        long records = caseDao.count(totalHql);
        DataTables dt = new DataTables();
        dt.setDraw(dataPage.getDraw());
        dt.setRecordsTotal(records);
        dt.setRecordsFiltered(records);
        dt.setData(tlist);
        return dt;
    }

    @Override
    public PCase getPCase(int id) {
        TCase tCase = caseDao.get(TCase.class,id);
        PCase pCase = new PCase();
        if(tCase != null){
            BeanUtils.copyProperties(tCase,pCase);
            return pCase;
        }
        return null;
    }

    @Override
    public void updateCase(PCase pCase, PAdmin pAdmin) {
        if(pCase.getId() == 0){  //添加
            TCase t = new TCase();
            BeanUtils.copyProperties(pCase,t);
            t.setCreateDate(new Date());
            t.setCreateUser(pAdmin.getUsername());
            caseDao.save(t);
        }else{
            TCase tCase = caseDao.get(TCase.class,pCase.getId());
            BeanUtils.copyProperties(pCase,tCase,new String[]{"createDate","createUser"});
        }

    }

    @Override
    public void delCase(int gid, HttpServletRequest request) {
        TCase t = caseDao.get(TCase.class,gid);
        String logoFile = t.getFilepath();
        if (StringUtils.isNotEmpty(logoFile)) {
            String realPath = request.getSession().getServletContext().getRealPath(logoFile);
            FileUtils.deleteQuietly(new File(realPath));
        }
        caseDao.delete(t);

    }

    @Override
    public PageView<TCase> CaseList(PageView<TCase> page, String sort) {
        if(sort == null || sort.equals("")){
            sort = "createDate desc";
        }
        String hql = "from TCase t";
        String totalHql = "select count(*) from TCase t";
        hql += " order by t."+sort;
        int total = (int)caseDao.count(totalHql);
        page = PageUtil.createPage(page,total);
        List<TCase> tlist = caseDao.find(hql,page.getCpage(), page.getSize());
        page.setRecords(tlist);
        return page;
    }

    @Override public List<TCase> getCaseList(int start, int end, String sort) {
        String hql = "from TCase t order by t."+sort;
        List<TCase> tlist = caseDao.findByPosition(hql, start, end);
        return tlist;
    }
}
